﻿using System.Xml;
using System.Collections.Generic;
using System;
using kad2SimSoft.Classes;
using kad2SimSoft.Resources.Lokalisierung;
using System.Windows.Forms;

namespace kad2SimSoft {
    public class Xml {
        string _filePathInput = kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString();
        string _filePathOuput = kad2SimSoft.Properties.Settings.Default["XMLExportPath"].ToString();

        private int _actualPeriod;

        private List<PurchasedPart> purchasedParts = new List<PurchasedPart>();
        private List<ManufacturedPart> manufacturedParts = new List<ManufacturedPart>();
        private List<Endproduct> endproducts = new List<Endproduct>();

        private List<Order> orders = new List<Order>();
        private List<Waitinglist> waitinglistStock = new List<Waitinglist>();
        private List<WaitinglistWorkplace> waitinglistWorkplace = new List<WaitinglistWorkplace>();
        private List<Workplace> workplaces = new List<Workplace>();
        private List<OrdersInWork> ordersInWork = new List<OrdersInWork>();

        private ManufacturedPart _manufacturedPart = null;
        private Endproduct _endproduct = null;
        private PurchasedPart _purchasedPart = null;
        private Workplace _workplace = null;
        private WaitinglistStock _waitinglistStock = null;
        private WaitinglistWorkplace _waitinglistWorkplace = null;
        private KeyData _keydata = null;
        private MainWindow _mainWindow = null;

        public MainWindow MainWindow {
            get { return _mainWindow; }
            set { _mainWindow = value; }
        }

        private XmlReader xmlReader;
        //private XmlWriter xmlWriter;
        public KeyData Keydata {
            get { return _keydata; }
            set { _keydata = value; }
        }

        public int ActualPeriod {
            get { return _actualPeriod; }
            set { _actualPeriod = value; }
        }

        public List<Endproduct> Endproducts {
            get { return endproducts; }
        }
        public List<ManufacturedPart> ManufacturedParts {
            get { return manufacturedParts; }
        }
        public List<PurchasedPart> PurchasedParts {
            get { return purchasedParts; }
        }
        public List<Order> Orders {
            get { return orders; }
        }
        public List<Waitinglist> WaitinglistStock {
            get { return waitinglistStock; }
        }
        public List<WaitinglistWorkplace> WaitinglistWorkplace {
            get { return waitinglistWorkplace; }
        }
        public List<Workplace> Workplaces {
            get { return workplaces; }
        }

        public List<OrdersInWork> OrdersInWork {
            get { return ordersInWork; }
            set { ordersInWork = value; }
        }
        /// <summary>
        /// Construktor
        /// </summary>
        /// <param name="filePathInput">path of scsim result file</param>
        /// <param name="filePathOutput">path where simsoft result file will be saved</param>
        public Xml (bool importXmlValues, MainWindow mainWindow) {
            if (importXmlValues) {
                _mainWindow = mainWindow;
                readActualPeriod();
                createRawProductList();
                readWarehouseStock();
                readWaitinglistStock();
                readWaitinglistWorkplace();
                readOrdersInWork();
                readKeyData();
                readFutureInwardStockMovement();
            }
        }

        /// <summary>
        /// Construktor
        /// </summary>
        /// <param name="filePathInput">path of scsim result file</param>
        /// <param name="filePathOutput">path where simsoft result file will be saved</param>
        public Xml (bool importXmlValues) {
            if (importXmlValues) {
                readActualPeriod();
                createRawProductList();
                readWarehouseStock();
                readWaitinglistStock();
                readWaitinglistWorkplace();
                readOrdersInWork();
                readKeyData();
            }
        }

        private void readActualPeriod () {

            int actPeriod;
            XmlDocument doc = new XmlDocument();
            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            actPeriod = Convert.ToInt32(doc.SelectSingleNode("/results/@period").Value) + 1;
            _actualPeriod = actPeriod;

        }

        private void readKeyData () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList keydataNode = null;
            _keydata = new KeyData();
            KeyDataList keydatalist = null;

            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            keydataNode = doc.SelectNodes("results").Item(0).SelectNodes("result").Item(0).SelectNodes("general");

            foreach (XmlNode data in keydataNode.Item(0)) {
                keydatalist = new KeyDataList();
                keydatalist.All = data.Attributes.Item(2).Value.ToString();
                keydatalist.Average = data.Attributes.Item(1).Value.ToString();
                keydatalist.CurrentValue = data.Attributes.Item(0).Value.ToString();
                switch (data.Name) {
                    case "capacity": _keydata.Capacity = keydatalist; break;
                    case "possiblecapacity": _keydata.Possiblecapacity = keydatalist; break;
                    case "relpossiblenormalcapacity": _keydata.Relpossiblenormalcapacity = keydatalist; break;
                    case "productivetime": _keydata.Productivetime = keydatalist; break;
                    case "effiency": _keydata.Effiency = keydatalist; break;
                    case "sellwish": _keydata.Sellwish = keydatalist; break;
                    case "salesquantity": _keydata.Salesquantity = keydatalist; break;
                    case "deliveryreliability": _keydata.Deliveryreliability = keydatalist; break;
                    case "idletime": _keydata.Idletime = keydatalist; break;
                    case "idletimecosts": _keydata.Idletimecosts = keydatalist; break;
                    case "storevalue": _keydata.Storevalue = keydatalist; break;
                    case "storagecosts": _keydata.Storagecosts = keydatalist; break;
                }
            }

            keydatalist = new KeyDataList();
            keydataNode = doc.SelectNodes("results").Item(0).SelectNodes("result").Item(0).SelectNodes("normalsale").Item(0).SelectNodes("salesprice");
            keydatalist.All = keydataNode.Item(0).Attributes.Item(2).Value.ToString();
            keydatalist.Average = keydataNode.Item(0).Attributes.Item(1).Value.ToString();
            keydatalist.CurrentValue = keydataNode.Item(0).Attributes.Item(0).Value.ToString();
            _keydata.Salesprice = keydatalist;

            keydatalist = new KeyDataList();
            keydataNode = doc.SelectNodes("results").Item(0).SelectNodes("result").Item(0).SelectNodes("normalsale").Item(0).SelectNodes("profit");
            keydatalist.All = keydataNode.Item(0).Attributes.Item(2).Value.ToString();
            keydatalist.Average = keydataNode.Item(0).Attributes.Item(1).Value.ToString();
            keydatalist.CurrentValue = keydataNode.Item(0).Attributes.Item(0).Value.ToString();
            _keydata.Profit = keydatalist;

            keydatalist = new KeyDataList();
            keydataNode = doc.SelectNodes("results").Item(0).SelectNodes("result").Item(0).SelectNodes("normalsale").Item(0).SelectNodes("profitperunit");
            keydatalist.All = keydataNode.Item(0).Attributes.Item(2).Value.ToString();
            keydatalist.Average = keydataNode.Item(0).Attributes.Item(1).Value.ToString();
            keydatalist.CurrentValue = keydataNode.Item(0).Attributes.Item(0).Value.ToString();
            _keydata.Profitperunit = keydatalist;

            keydatalist = new KeyDataList();
            keydataNode = doc.SelectNodes("results").Item(0).SelectNodes("result").Item(0).SelectNodes("marketplacesale").Item(0).SelectNodes("profit");
            keydatalist.All = keydataNode.Item(0).Attributes.Item(2).Value.ToString();
            keydatalist.Average = keydataNode.Item(0).Attributes.Item(1).Value.ToString();
            keydatalist.CurrentValue = keydataNode.Item(0).Attributes.Item(0).Value.ToString();
            _keydata.Marketplacesale = keydatalist;
        }

        private void readOrdersInWork () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList ordersinworkNode = null;

            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            ordersinworkNode = doc.SelectNodes("results").Item(0).SelectNodes("ordersinwork");

            foreach (XmlNode orderinwork in ordersinworkNode.Item(0)) {
                OrdersInWork orderInWork = new OrdersInWork();
                orderInWork.Workplace = Convert.ToInt32(orderinwork.Attributes.Item(0).Value.ToString());
                orderInWork.Period = Convert.ToInt32(orderinwork.Attributes.Item(1).Value.ToString());
                orderInWork.OrderId = Convert.ToInt32(orderinwork.Attributes.Item(2).Value.ToString());
                orderInWork.Batch = Convert.ToInt32(orderinwork.Attributes.Item(3).Value.ToString());
                orderInWork.Item = Convert.ToInt32(orderinwork.Attributes.Item(4).Value.ToString());
                orderInWork.Amount = Convert.ToInt32(orderinwork.Attributes.Item(5).Value.ToString());
                orderInWork.Timeneed = Convert.ToInt32(orderinwork.Attributes.Item(6).Value.ToString());
                ordersInWork.Add(orderInWork);
            }
        }

        private void readWaitinglistWorkplace () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList waitinglistsWorkplaces = null;

            List<SettingUpTime> settingUpTime;

            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            waitinglistsWorkplaces = doc.SelectNodes("results").Item(0).SelectNodes("waitinglistworkstations");

            foreach (XmlNode workplace in waitinglistsWorkplaces.Item(0)) {
                _workplace = new Workplace();
                waitinglistWorkplace = new List<WaitinglistWorkplace>();
                _workplace.Id = Convert.ToInt32(workplace.Attributes.Item(0).Value.ToString());

                //Den Arbeitsplätzen die Produktionszeiten aller Fertigungsteile zuordnen
                #region Produktionszeiten

                settingUpTime = new List<SettingUpTime>();

                switch (_workplace.Id) {

                    case 1:
                        settingUpTime.Add(new SettingUpTime(49, 20, 6));
                        settingUpTime.Add(new SettingUpTime(54, 20, 6));
                        settingUpTime.Add(new SettingUpTime(29, 20, 6));
                        break;
                    case 2:
                        settingUpTime.Add(new SettingUpTime(50, 20, 5));
                        settingUpTime.Add(new SettingUpTime(55, 30, 5));
                        settingUpTime.Add(new SettingUpTime(30, 20, 5));
                        break;
                    case 3:
                        settingUpTime.Add(new SettingUpTime(51, 20, 5));
                        settingUpTime.Add(new SettingUpTime(56, 20, 6));
                        settingUpTime.Add(new SettingUpTime(31, 20, 6));
                        break;
                    case 4:
                        settingUpTime.Add(new SettingUpTime(1, 30, 6));
                        settingUpTime.Add(new SettingUpTime(2, 20, 7));
                        settingUpTime.Add(new SettingUpTime(3, 30, 7));
                        break;
                    case 6:
                        settingUpTime.Add(new SettingUpTime(16, 15, 2));
                        settingUpTime.Add(new SettingUpTime(18, 15, 3));
                        settingUpTime.Add(new SettingUpTime(19, 15, 3));
                        settingUpTime.Add(new SettingUpTime(20, 15, 3));
                        break;
                    case 7:
                        settingUpTime.Add(new SettingUpTime(10, 20, 2));
                        settingUpTime.Add(new SettingUpTime(11, 20, 2));
                        settingUpTime.Add(new SettingUpTime(12, 20, 2));
                        settingUpTime.Add(new SettingUpTime(13, 20, 2));
                        settingUpTime.Add(new SettingUpTime(14, 20, 2));
                        settingUpTime.Add(new SettingUpTime(15, 20, 2));
                        settingUpTime.Add(new SettingUpTime(18, 20, 2));
                        settingUpTime.Add(new SettingUpTime(19, 20, 2));
                        settingUpTime.Add(new SettingUpTime(20, 20, 2));
                        settingUpTime.Add(new SettingUpTime(26, 20, 2));
                        break;
                    case 8:
                        settingUpTime.Add(new SettingUpTime(10, 15, 1));
                        settingUpTime.Add(new SettingUpTime(11, 15, 2));
                        settingUpTime.Add(new SettingUpTime(12, 15, 2));
                        settingUpTime.Add(new SettingUpTime(13, 15, 1));
                        settingUpTime.Add(new SettingUpTime(14, 15, 2));
                        settingUpTime.Add(new SettingUpTime(15, 15, 2));
                        settingUpTime.Add(new SettingUpTime(18, 20, 3));
                        settingUpTime.Add(new SettingUpTime(19, 25, 3));
                        settingUpTime.Add(new SettingUpTime(20, 20, 3));
                        break;
                    case 9:
                        settingUpTime.Add(new SettingUpTime(10, 15, 3));
                        settingUpTime.Add(new SettingUpTime(11, 15, 3));
                        settingUpTime.Add(new SettingUpTime(12, 15, 3));
                        settingUpTime.Add(new SettingUpTime(13, 15, 3));
                        settingUpTime.Add(new SettingUpTime(14, 15, 3));
                        settingUpTime.Add(new SettingUpTime(15, 15, 3));
                        settingUpTime.Add(new SettingUpTime(18, 15, 2));
                        settingUpTime.Add(new SettingUpTime(19, 20, 2));
                        settingUpTime.Add(new SettingUpTime(20, 15, 2));
                        break;
                    case 10:
                        settingUpTime.Add(new SettingUpTime(4, 20, 4));
                        settingUpTime.Add(new SettingUpTime(5, 20, 4));
                        settingUpTime.Add(new SettingUpTime(6, 20, 4));
                        settingUpTime.Add(new SettingUpTime(7, 20, 4));
                        settingUpTime.Add(new SettingUpTime(8, 20, 4));
                        settingUpTime.Add(new SettingUpTime(9, 20, 4));
                        break;
                    case 11:
                        settingUpTime.Add(new SettingUpTime(4, 10, 3));
                        settingUpTime.Add(new SettingUpTime(5, 10, 3));
                        settingUpTime.Add(new SettingUpTime(6, 20, 3));
                        settingUpTime.Add(new SettingUpTime(7, 20, 3));
                        settingUpTime.Add(new SettingUpTime(8, 20, 3));
                        settingUpTime.Add(new SettingUpTime(9, 20, 3));
                        break;
                    case 12:
                        settingUpTime.Add(new SettingUpTime(10, 0, 3));
                        settingUpTime.Add(new SettingUpTime(11, 0, 3));
                        settingUpTime.Add(new SettingUpTime(12, 0, 3));
                        settingUpTime.Add(new SettingUpTime(13, 0, 3));
                        settingUpTime.Add(new SettingUpTime(14, 0, 3));
                        settingUpTime.Add(new SettingUpTime(15, 0, 3));
                        break;
                    case 13:
                        settingUpTime.Add(new SettingUpTime(10, 0, 2));
                        settingUpTime.Add(new SettingUpTime(11, 0, 2));
                        settingUpTime.Add(new SettingUpTime(12, 0, 2));
                        settingUpTime.Add(new SettingUpTime(13, 0, 2));
                        settingUpTime.Add(new SettingUpTime(14, 0, 2));
                        settingUpTime.Add(new SettingUpTime(15, 0, 2));
                        break;
                    case 14:
                        settingUpTime.Add(new SettingUpTime(16, 0, 3));
                        break;
                    case 15:
                        settingUpTime.Add(new SettingUpTime(17, 15, 3));
                        settingUpTime.Add(new SettingUpTime(26, 15, 3));
                        break;

                }


                //Liste Prouktionzeit dem Arbeitsplatz zuordnen
                _workplace.SettingUpTime = settingUpTime;

                #endregion Produktionszeiten

                foreach (XmlNode waitinglistAtWorkplace in workplace) {
                    _waitinglistWorkplace = new WaitinglistWorkplace();
                    _waitinglistWorkplace.Amount = Convert.ToInt32(waitinglistAtWorkplace.Attributes.Item(5).Value.ToString());
                    _waitinglistWorkplace.Timeneed = Convert.ToInt32(waitinglistAtWorkplace.Attributes.Item(6).Value.ToString());
                    _waitinglistWorkplace.Item = Convert.ToInt32(waitinglistAtWorkplace.Attributes.Item(4).Value.ToString());
                    _waitinglistWorkplace.LastBatch = Convert.ToInt32(waitinglistAtWorkplace.Attributes.Item(3).Value.ToString());
                    _waitinglistWorkplace.FirstBatch = Convert.ToInt32(waitinglistAtWorkplace.Attributes.Item(2).Value.ToString());
                    _waitinglistWorkplace.Order = Convert.ToInt32(waitinglistAtWorkplace.Attributes.Item(1).Value.ToString());
                    waitinglistWorkplace.Add(_waitinglistWorkplace);
                    _waitinglistWorkplace = null;
                }
                _workplace.WaitingList = waitinglistWorkplace;
                waitinglistWorkplace = null;
                workplaces.Add(_workplace);
                _workplace = null;
            }
        }


        public void readWarehouseStock () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList articlelistNode = null;

            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            articlelistNode = doc.SelectNodes("results").Item(0).SelectNodes("warehousestock");

            foreach (Endproduct endproduct in endproducts) {
                foreach (XmlNode article in articlelistNode.Item(0)) {
                    if (article.Name != "totalstockvalue") {
                        if (endproduct.Id == Convert.ToInt32(article.Attributes.Item(0).Value.ToString())) {
                            endproduct.Amount = Convert.ToInt32(article.Attributes.Item(1).Value.ToString());
                            endproduct.Price = Convert.ToDouble(article.Attributes.Item(4).Value.ToString());
                        }
                    }
                }
            }

            foreach (ManufacturedPart manufacturedPart in manufacturedParts) {
                foreach (XmlNode article in articlelistNode.Item(0)) {
                    if (article.Name != "totalstockvalue") {
                        if (manufacturedPart.Id == Convert.ToInt32(article.Attributes.Item(0).Value.ToString())) {
                            manufacturedPart.Amount = Convert.ToInt32(article.Attributes.Item(1).Value.ToString());
                            manufacturedPart.Price = Convert.ToDouble(article.Attributes.Item(4).Value.ToString());
                        }
                    }
                }
            }

            foreach (PurchasedPart purchasedPart in purchasedParts) {
                foreach (XmlNode article in articlelistNode.Item(0)) {
                    if (article.Name != "totalstockvalue") {
                        if (purchasedPart.Id == Convert.ToInt32(article.Attributes.Item(0).Value.ToString())) {
                            purchasedPart.Amount = Convert.ToInt32(article.Attributes.Item(1).Value.ToString());
                            purchasedPart.Price = Convert.ToDouble(article.Attributes.Item(4).Value.ToString());
                        }
                    }
                }
            }
        }

        public void readFutureInwardStockMovement () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList articlelistNode = null;

            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            articlelistNode = doc.SelectNodes("results").Item(0).SelectNodes("futureinwardstockmovement");

            //foreach (Endproduct endproduct in endproducts) {
            //    foreach (XmlNode article in articlelistNode.Item(0)) {
            //        if (article.Name != "totalstockvalue") {
            //            if (endproduct.Id == Convert.ToInt32(article.Attributes.Item(0).Value.ToString())) {
            //                endproduct.Amount = Convert.ToInt32(article.Attributes.Item(1).Value.ToString());
            //                endproduct.Price = Convert.ToDouble(article.Attributes.Item(4).Value.ToString());
            //            }
            //        }
            //    }
            //}

            //foreach (ManufacturedPart manufacturedPart in manufacturedParts) {
            //    foreach (XmlNode article in articlelistNode.Item(0)) {
            //        if (article.Name != "totalstockvalue") {
            //            if (manufacturedPart.Id == Convert.ToInt32(article.Attributes.Item(0).Value.ToString())) {
            //                manufacturedPart.Amount = Convert.ToInt32(article.Attributes.Item(1).Value.ToString());
            //                manufacturedPart.Price = Convert.ToDouble(article.Attributes.Item(4).Value.ToString());
            //            }
            //        }
            //    }
            //}

            foreach (PurchasedPart purchasedPart in purchasedParts) {
                foreach (XmlNode article in articlelistNode.Item(0)) {
                    if (purchasedPart.Id == Convert.ToInt32(article.Attributes.Item(3).Value.ToString())) {
                        purchasedPart.OrderPeriod = Convert.ToInt32(article.Attributes.Item(0).Value.ToString());
                        purchasedPart.FutureInWardAmount = Convert.ToInt32(article.Attributes.Item(4).Value.ToString());
                    }
                }
            }
        }

        public void readWaitinglistStock () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList waitinglistStockNode = null;
            doc.Load(kad2SimSoft.Properties.Settings.Default["XMLImportPath"].ToString());
            waitinglistStockNode = doc.SelectNodes("results").Item(0).SelectNodes("waitingliststock");
            _waitinglistStock = new WaitinglistStock();

            foreach (XmlNode missingPart in waitinglistStockNode.Item(0)) {
                _waitinglistStock.MissingPartId = Convert.ToInt32(missingPart.Attributes.Item(0).Value.ToString());
                foreach (XmlNode entry in missingPart.ChildNodes) {
                    _waitinglistStock.Amount = Convert.ToInt32(entry.Attributes.Item(5).Value.ToString());
                    _waitinglistStock.Item = Convert.ToInt32(entry.Attributes.Item(4).Value.ToString());
                    _waitinglistStock.LastBatch = Convert.ToInt32(entry.Attributes.Item(3).Value.ToString());
                    _waitinglistStock.FirstBatch = Convert.ToInt32(entry.Attributes.Item(2).Value.ToString());
                    _waitinglistStock.Order = Convert.ToInt32(entry.Attributes.Item(1).Value.ToString());
                    waitinglistStock.Add(_waitinglistStock);
                    _waitinglistStock = new WaitinglistStock();
                }
            }
        }

        public void readIdleTimeCosts () {
            ///prüfung ob teil erzeugnis oder kaufteil hinzufügen
            xmlReader = XmlReader.Create(_filePathInput);
            while (xmlReader.Read()) {
                switch (xmlReader.Name) {
                    case "idletimecosts":
                        while (xmlReader.Read()) {
                            switch (xmlReader.Name) {
                                case "worklplace":
                                    while (xmlReader.MoveToNextAttribute()) {
                                    }
                                    if (xmlReader.Name == "id") {
                                        _workplace = new Workplace();
                                        _workplace.Id = Convert.ToInt32(xmlReader.Value);
                                    }
                                    workplaces.Add(_workplace);
                                    break;
                            }
                        }
                        break;
                }
            }
        }

        public void createRawProductList () {
            XmlDocument doc = new XmlDocument();
            XmlNodeList articlelistNode = null;
            doc.Load("Resources/Listen/Articlelist.xml");
            articlelistNode = doc.SelectNodes("articlelist");
            foreach (XmlNode article in articlelistNode.Item(0)) {
                switch (article.Attributes.Item(1).Value.ToString()) {
                    case "P":
                        _endproduct = new Endproduct();
                        _endproduct.Id = Convert.ToInt32(article.Attributes.Item(0).Value.ToString());
                        _endproduct.StartAmount = Convert.ToInt32(article.Attributes.Item(2).Value.ToString());
                        //_endproduct.Price = double.Parse(article.Attributes.Item(3).Value.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                        endproducts.Add(_endproduct);
                        break;
                    case "E":
                        _manufacturedPart = new ManufacturedPart();
                        _manufacturedPart.Id = Convert.ToInt32(article.Attributes.Item(0).Value.ToString());
                        _manufacturedPart.StartAmount = Convert.ToInt32(article.Attributes.Item(2).Value.ToString());
                        //_manufacturedPart.Price = double.Parse(article.Attributes.Item(3).Value.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                        manufacturedParts.Add(_manufacturedPart);
                        break;
                    case "K":
                        _purchasedPart = new PurchasedPart();
                        _purchasedPart.Id = Convert.ToInt32(article.Attributes.Item(0).Value.ToString());
                        _purchasedPart.StartAmount = Convert.ToInt32(article.Attributes.Item(2).Value.ToString());
                        //_purchasedPart.Price = double.Parse(article.Attributes.Item(3).Value.ToString(), System.Globalization.CultureInfo.InvariantCulture);

                        _purchasedPart.UsedInP1 = Convert.ToInt32(article.SelectNodes("usedlist").Item(0).SelectNodes("article").Item(0).Attributes.Item(1).Value.ToString());
                        _purchasedPart.UsedInP2 = Convert.ToInt32(article.SelectNodes("usedlist").Item(0).SelectNodes("article").Item(1).Attributes.Item(1).Value.ToString());
                        _purchasedPart.UsedInP3 = Convert.ToInt32(article.SelectNodes("usedlist").Item(0).SelectNodes("article").Item(2).Attributes.Item(1).Value.ToString());
                        //_purchasedPart.UsedInP2= ;
                        //_purchasedPart.UsedInP3 ;

                        _purchasedPart.DiscountQuantity = Convert.ToInt32(article.Attributes.Item(4).Value.ToString());
                        _purchasedPart.OrderCosts = double.Parse(article.Attributes.Item(5).Value.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                        _purchasedPart.ProcureLeadTime = double.Parse(article.Attributes.Item(6).Value.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                        _purchasedPart.OrderDeviation = double.Parse(article.Attributes.Item(7).Value.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                        purchasedParts.Add(_purchasedPart);
                        break;
                }
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public void writeXml (MainWindow window) {
            String savePath;
            /// Open Save Dialog and first xml elements.
            savePath = openSaveFileDialog();
            if (savePath.Equals("")) {
                return;
            }
            XmlWriter writer = XmlWriter.Create(savePath);
            writer.WriteStartElement("input");
            writer.WriteStartElement("qualitycontrol");
            writer.WriteAttributeString("type", "no");
            writer.WriteAttributeString("losequantity", "0");
            writer.WriteAttributeString("delay", "0");

            writer.WriteEndElement();

            ///Sellwish part

            /// implement here the forecast values for the current period.
            writer.WriteStartElement("sellwish");
            List<Forecast> forecasts = new List<Forecast>();
            forecasts = window.ForecastControl.Forecasts;
            foreach (Forecast sellwish in forecasts) {

                if (sellwish.Period == 0) {

                    writer.WriteStartElement("item");
                    writer.WriteAttributeString("article", sellwish.Product.Id.ToString());
                    writer.WriteAttributeString("quantity", sellwish.Count.ToString());
                    writer.WriteEndElement();
                }
            }

            writer.WriteEndElement();


            /// implement here selldirect wishes.
            writer.WriteStartElement("selldirect");
            List<AdditionalOrder> additionalOrders = new List<AdditionalOrder>();
            additionalOrders = window.ForecastControl.AdditionalOrder;
            foreach (AdditionalOrder additionlOrder in additionalOrders) {
                writer.WriteStartElement("item");
                writer.WriteAttributeString("price", additionlOrder.Price.ToString());
                writer.WriteAttributeString("penalty", additionlOrder.Penalty.ToString());
                writer.WriteAttributeString("article", additionlOrder.Product.ToString());
                writer.WriteAttributeString("quantity", additionlOrder.Quantity.ToString());

                writer.WriteEndElement();
            }
            writer.WriteEndElement();

            ///Implement orderlist get here
            writer.WriteStartElement("orderlist");

            foreach (PurchasedPart part in window.PurchasingView.PurchasedParts) {
                if (part.OrderAmount > 0) {
                    writer.WriteStartElement("order");
                    writer.WriteAttributeString("quantity", part.OrderAmount.ToString());
                    writer.WriteAttributeString("article", part.Id.ToString());
                    writer.WriteAttributeString("modus", part.OrderModus.ToString());
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            ///implement productionlist get here
            writer.WriteStartElement("productionlist");
            List<Order> orders = new List<Order>();
            orders = window.ProductionOrderView.getSortedOrderList();
            foreach (Order order in orders) {
                writer.WriteStartElement("production");
                writer.WriteAttributeString("quantity", order.Amount.ToString());
                writer.WriteAttributeString("article", order.Product.Id.ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement();

            ///Implement workingtimelist get here
            writer.WriteStartElement("workingtimelist");
            List<Capacity> capacity = new List<Capacity>();
            capacity = window.Capacity;
            foreach (Capacity c in capacity) {
                writer.WriteStartElement("workingtime");
                writer.WriteAttributeString("station", c.WorkplaceId);
                writer.WriteAttributeString("shift", c.Shift);
                writer.WriteAttributeString("overtime", Math.Round((Convert.ToInt32(c.Overtime)/5.0)).ToString());
                writer.WriteEndElement();
            }

            writer.WriteEndElement();

            /// End of Element "input" and closing of xml document
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();

            System.Diagnostics.Process.Start(savePath);
        }


        private String openSaveFileDialog () {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "XML Document|*.xml";
            saveDialog.Title = enUS.titleSaveFileDialog;
            saveDialog.ShowDialog();
            if (saveDialog.FileName != "") {
                return saveDialog.FileName;
            }
            return "";
        }
    }
}
